<?php

/**
 * fetch the cash bank tx type
 */
$CLFetchers->fetchCbTxType($Db,
                           $_GET["C_B_CODE"],
                           $srcDbMstCb,
                           $cbTxType);

$CLocal->CbTxType($_POST["typeEnt"],
                  $cbTxType,
                  $txType);

if($_POST["typeEnt"] == _CASH_BANK_IN){

  if($_POST["txEnt"] == _CASH_BANK_INVOICE_NAME){

    /**
     * check out if entry exists then
     * it is a modified type
     * then clean up
     */
    $CLFetchers->fetchTmpDtlInvoice($Db,
                                    $_SESSION["tmpJid"],
                                    $srcDbTmpDtlIvcCustomer,
                                    $nData,
                                    $lstData);

    if($nData > 0){

      $CHandlers->dbEntDelete($Db,
                              $_SESSION["tmpJid"],
                              $srcDbTmpSubIvcCustomer);

      $CHandlers->dbEntDelete($Db,
                              $_SESSION["tmpJid"],
                              $srcDbTmpDtlIvcCustomer);
    }else{

      /*Do nothing*/
    }

    for($iRow = 0; $iRow < $_SESSION["init_sub"] ; $iRow++){

      $txDate  = " TO_DATE('".$_POST["trDateEnt"][$iRow]."','dd-mm-YYYY') ";
      $dtxDate = " TO_DATE('".$_POST["dueDateEnt"][$iRow]."','dd-mm-YYYY') ";

      $values = " '".$_SESSION["tmpJid"]."',          ".
                "  ".($iRow+1)." ,                    ".
                "  ".$curPeriode." ,                  ".
                " '".$_POST["notaSrcEnt"][$iRow]."',  ".
                "  ".$txDate." ,                      ".
                "  ".$dtxDate." ,                     ".
                " '".$_GET["whoseCode"]."',           ".
                " '".$_POST["currencyEnt"][$iRow]."', ".
                "  ".$_POST["rateEnt"][$iRow]." ,     ".
                "  ".$_POST["remainEnt"][$iRow]." ,   ".
                "  ".$_POST["receiveEnt"][$iRow]." ,  ".
                "  ".$txType."                        ";

      $CLHandlers->insToTmpSub($Db,
                               $values,
                               $srcDbTmpSubIvcCustomer);
    }

    $values = " '".$_SESSION["tmpJid"]."',   ".
              "  ".$_POST["totInvoice"]." ,  ".
              "  ".$_GET["totSrcNet"]." ,    ".
              "  ".$_POST["gainEnt"]." ,     ".
              "  ".$_POST["discEnt"]."       ";

    $CLHandlers->insToTmpDtl($Db,
                             $values,
                             $srcDbTmpDtlIvcCustomer);
  }else
  if($_POST["txEnt"] == _CASH_BANK_CCL_DP_NAME){


  }else{

    $Misc->usrDieJs("Error: Unknown Tx");
  }
}else
if($_POST["typeEnt"] == _CASH_BANK_OUT){

  if($_POST["txEnt"] == _CASH_BANK_INVOICE_NAME){

    /**
     * check out if entry exists then
     * it is a modified type
     * then clean up
     */
    $CLFetchers->fetchTmpDtlInvoice($Db,
                                    $_SESSION["tmpJid"],
                                    $srcDbTmpDtlIvcSupplier,
                                    $nData,
                                    $lstData);

    if($nData > 0){

      $CHandlers->dbEntDelete($Db,
                              $_SESSION["tmpJid"],
                              $srcDbTmpSubIvcSupplier);

      $CHandlers->dbEntDelete($Db,
                              $_SESSION["tmpJid"],
                              $srcDbTmpDtlIvcSupplier);
    }else{

      /*Do nothing*/
    }

    for($iRow = 0; $iRow < $_SESSION["init_sub"] ; $iRow++){

      $txDate  = " TO_DATE('".$_POST["trDateEnt"][$iRow]."','dd-mm-YYYY') ";
      $dtxDate = " TO_DATE('".$_POST["dueDateEnt"][$iRow]."','dd-mm-YYYY') ";

      $values = " '".$_SESSION["tmpJid"]."',          ".
                "  ".($iRow+1)." ,                    ".
                "  ".$curPeriode." ,                  ".
                " '".$_POST["notaSrcEnt"][$iRow]."',  ".
                "  ".$txDate." ,                      ".
                "  ".$dtxDate." ,                     ".
                " '".$_GET["whoseCode"]."',           ".
                " '".$_POST["currencyEnt"][$iRow]."', ".
                "  ".$_POST["rateEnt"][$iRow]." ,     ".
                "  ".$_POST["remainEnt"][$iRow]." ,   ".
                "  ".$_POST["paymentEnt"][$iRow]." ,  ".
                "  ".$txType."                        ";

      $CLHandlers->insToTmpSub($Db,
                               $values,
                               $srcDbTmpSubIvcSupplier);
    }

    $values = " '".$_SESSION["tmpJid"]."',   ".
              "  ".$_POST["totInvoice"]." ,  ".
              "  ".$_GET["totSrcNet"]." ,    ".
              "  ".$_POST["gainEnt"]." ,     ".
              "  ".$_POST["discEnt"]."       ";

    $CLHandlers->insToTmpDtl($Db,
                             $values,
                             $srcDbTmpDtlIvcSupplier);
  }else
  if($_POST["txEnt"] == _CASH_BANK_CCL_DP_NAME){

    /**
     * check out if entry exists then
     * it is a modified type
     * then clean up
     */
    $CLFetchers->fetchTmpDtlDp($Db,
                               $_SESSION["tmpJid"],
                               $SourceDb->fetchValue("srcDbTmpDtlCclDp"),
                               $nData,
                               $lstData);

    if($nData > 0){

      $CHandlers->dbEntDelete($Db,
                              $_SESSION["tmpJid"],
                              $SourceDb->fetchValue("srcDbTmpDtlCclDp"));

      $CHandlers->dbEntDelete($Db,
                              $_SESSION["tmpJid"],
                              $SourceDb->fetchValue("srcDbTmpSubCclDp"));
    }else{

      /*Do nothing*/
    }

    /**
     * we need to neutralize amount and txType
     * where amount done by sub and detail calculation
     *
     * refer to tocmenu_31/controllers/procAdd.php:229
     * a -1 as neutralized has been added
     *
     * and so with the txType, we knew that this is
     * an OUT type than we just opposite it to IN type
     */
    $CLocal->CbTxType(_CASH_BANK_IN,
                      $cbTxType,
                      $txType);

    for($iRow = 0; $iRow < $_SESSION["init_sub"] ; $iRow++){

      $txDate  = " TO_DATE('".$_POST["trDateEnt"][$iRow]."','dd-mm-YYYY') ";
      $dtxDate = " TO_DATE('".$_POST["dueDateEnt"][$iRow]."','dd-mm-YYYY') ";

      $values = " '".$_SESSION["tmpJid"]."',          ".
                "  ".($iRow+1)." ,                    ".
                "  ".$curPeriode." ,                  ".
                " '".$_POST["notaSrcEnt"][$iRow]."',  ".
                " '".$_POST["cbSource"][$iRow]."',    ".
                "  ".$txDate." ,                      ".
                "  ".$dtxDate." ,                     ".
                " '".$_GET["whoseCode"]."',           ".
                " '".$_POST["currencyEnt"][$iRow]."', ".
                "  ".$_POST["rateEnt"][$iRow]." ,     ".
                "  ".$_POST["remainEnt"][$iRow]." ,   ".
                "  ".$_POST["paymentEnt"][$iRow]." ,  ".
                "  ".$txType."                        ";

      $CLHandlers->insToTmpSubDp($Db,
                                 $values,
                                 $SourceDb->fetchValue("srcDbTmpSubCclDp"));
    }

    $values = " '".$_SESSION["tmpJid"]."',   ".
              "  ".$_POST["totInvoice"]." ,  ".
              "  ".$_GET["totSrcNet"]." ,    ".
              "  ".$_POST["gainEnt"]." ,     ".
              "  ".$_POST["discEnt"]."       ";

    $CLHandlers->insToTmpDtl($Db,
                             $values,
                             $SourceDb->fetchValue("srcDbTmpDtlCclDp"));
  }else{

    $Misc->usrDieJs("Error: Unknown Tx");
  }
}else{

  $Misc->usrDieJs("Error: Unknown Type");
}

$Db->commit();

if($Db->getStatus()){

  echo  $GJs->jsScript(_TYPE_JS,"this.window.close();");
}else{

  $Misc->usrMsgJs("Upload Process Interupted");
}

?>